package com.alex.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alex.service.*;
import com.alex.util.TipInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.alex.entity.Board;
import com.alex.entity.Tip;
import com.alex.entity.User;

public class TipAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private int boardId;
	private String title;
	private String content;
	private int topicId;
	
	List<TipInfo> info;
	private IUserService userService;
	private ITipService tipService;
	private IReplyService replyService;
	private IBoardService boardService;
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public ITipService getTipService() {
		return tipService;
	}

	public void setTipService(ITipService tipService) {
		this.tipService = tipService;
	}

	public IReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(IReplyService replyService) {
		this.replyService = replyService;
	}

	public IBoardService getBoardService() {
		return boardService;
	}

	public void setBoardService(IBoardService boardService) {
		this.boardService = boardService;
	}

	public List<TipInfo> getInfo() {
		return info;
	}

	public void setInfo(List<TipInfo> info) {
		this.info = info;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}


	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getBoardId() {
		return boardId;
	}

	public void setBoardId(int boardId) {
		this.boardId = boardId;
	}

	/**
	 * 添加帖子
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addTip() throws Exception {
		info=new ArrayList<TipInfo>();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		ActionContext contents = ActionContext.getContext();
		Map session = contents.getSession();
		User user = (User) session.get("user");
		int uid = user.getUserid();
		System.out.println(uid);
		Board board = boardService.findBoardByid(boardId);
		int bid=board.getBoardid();
		Tip tip = new Tip();
		tip.setTitle(title);
		tip.setContent(content);
//		tip.setBoard(board);
//		tip.setUser(user);
		tip.setBoardid(bid);
		tip.setUserid(uid);
		tip.setPublishtime(sdf.format(date));
		tipService.save(tip);
		
		List listTip = tipService.findListTip(boardId); 
		TipInfo tipinfo = null;
		for (int i = 0; i < listTip.size(); i++) {
			Tip tip2 = (Tip) listTip.get(i); 
			User user2 = userService.findById(tip2.getUser().getUserid()); 
			tipinfo = new TipInfo();
			tipinfo.setTitle(tip2.getTitle());
			tipinfo.setReplyCount(replyService.findCountReply(tip2.getTipid()));
			tipinfo.setUsername(user2.getUsername());
			tipinfo.setTipid(tip2.getTipid());
			info.add(tipinfo);
		}
		ActionContext contents2 = ActionContext.getContext();
		Map request = (Map) contents2.get("request");
		request.put("boardId", boardId);
		return SUCCESS;
	}

	/**
	 * 删除帖子
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteTip() throws Exception {
		
		tipService.delete(topicId);
		return SUCCESS;
	}

	/**
	 * 查找帖子根据用户
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findListTipByuserid() throws Exception {
		info=new ArrayList<TipInfo>();
		ActionContext contents = ActionContext.getContext();
		Map session = contents.getSession();
		User user = (User) session.get("user");
		int uid = user.getUserid();
		
		List userAllTips = tipService.findListTipByuserid(user.getUserid());
		TipInfo tipinfo = null;
		for (int i = 0; i < userAllTips.size(); i++) {
			Tip tip = (Tip) userAllTips.get(i); 
			tipinfo = new TipInfo();
			tipinfo.setTitle(tip.getTitle());
			tipinfo.setReplyCount(replyService.findCountReply(tip.getTipid()));
			tipinfo.setTipid(tip.getTipid());
			info.add(tipinfo);
		}
		return SUCCESS;
	}

	/**
	 * 通过板块寻找帖子
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findTipsByBoard() throws Exception {
		info= new ArrayList<TipInfo>();
		Board board = boardService.findBoardByid(boardId); 
		List listTip = tipService.findListTip(boardId); 
		TipInfo tipinfo = null;
		for (int i = 0; i < listTip.size(); i++) {
			Tip tip = (Tip) listTip.get(i); 
			User user = userService.findById(tip.getUser().getUserid()); 
			tipinfo = new TipInfo();
			tipinfo.setTitle(tip.getTitle());
			tipinfo.setReplyCount(replyService.findCountReply(tip.getTipid()));
			tipinfo.setUsername(user.getUsername());
			tipinfo.setTipid(tip.getTipid());
			info.add(tipinfo);
		}
		ActionContext contents = ActionContext.getContext();
		Map request = (Map) contents.get("request");
		request.put("boardId", boardId);
		return SUCCESS;
	}
}
